<template>
  <div class="container">
    <!--  主题内容标题   -->
    <div class="flex contain-header">
      <div class="contain-title">关注和粉丝</div>
    </div>

    <!--  表格头部标签页  -->
    <div class="tag-page">
      <a-tabs default-active-key="1" @change="callback">
        <!--    我的关注      -->
        <a-tab-pane key="1" tab="我的关注">
          <a-list item-layout="vertical" size="large" :loading="isLoading" :pagination="pagination" :data-source="followlist">
            <a-list-item slot="renderItem" key="item.title" slot-scope="item, index">
              <div slot="extra">
                <div class="more-list">
                  <div class="more">
                    更多 >>
                  </div>
                </div>
              </div>
              <a-list-item-meta :description="item.stSchool + item.stCollege + (item.userRole === 0 ? '学生': '老师')">
                <div slot="title" class="header">
                  <div class="title">
                    <div>{{ item.userName }}</div>
                    <div class="course">粉丝数： {{item.fansNum}}</div>
                    <a-tag color="#1bcbb4">
                      {{item.isFollowed === 0 ? '未关注' : '已关注'}}
                    </a-tag>
                  </div>
                </div>
                <a-avatar slot="avatar" :src="'http://zctuchuang.oss-cn-shenzhen.aliyuncs.com/'+item.userFace" class="avatar"/>
              </a-list-item-meta>
              <div class="followed" @click="cancelFollowed(item)">
                <a-button>取消关注</a-button>
              </div>
            </a-list-item>
          </a-list>
        </a-tab-pane>
        <!--   我的粉丝     -->
        <a-tab-pane key="2" tab="我的粉丝" force-render>
          <a-list :grid="{ gutter: 6, column: 3 }" :loading="isFanLoading" :data-source="fanList" :pagination="fanPagination">
            <a-list-item slot="renderItem" slot-scope="item, index" class="column-space">
              <a-list-item-meta :description="item.stSchool + item.stCollege + (item.userRole === 0 ? '学生': '老师')">
                <div slot="title" class="header">
                  <div class="title">
                    <div>{{ item.userName }}</div>
                    <div class="course">粉丝数： {{item.fansNum}}</div>
                    <a-tag color="#1bcbb4">
                      {{item.isFollowed === 0 ? '未关注' : '已关注'}}
                    </a-tag>
                  </div>
                </div>
                <a-avatar slot="avatar" :src="'http://zctuchuang.oss-cn-shenzhen.aliyuncs.com/'+item.userFace" class="avatar"/>
              </a-list-item-meta>
              <div class="followed" v-if="item.isFollowed" @click="cancelFollowed(item)">
                <a-button>取消关注</a-button>
              </div>
              <div class="followed" v-else @click="follow(item)">
                <a-button>关注</a-button>
              </div>
            </a-list-item>
          </a-list>
        </a-tab-pane>
      </a-tabs>
    </div>
  </div>
</template>

<script>
import {getFollowList, cancelFollowResource, followResource} from "@/api";

  export default {
    name: "FollowsFans.vue",
    data() {
      return {
        pagination: {
          total: 100,
          defaultPageSize: 10,
          showTotal: total => `共 ${total} 条数据`,
          showQuickJumper: true,
          showSizeChanger: true,
          pageSizeOptions: ['5', '10', '15', '20'],
          onChange:(page,pageSize)=>this.getFollowsList(page,pageSize),//点击页码事件
        },
        fanPagination: {
          total: 100,
          defaultPageSize: 10,
          showTotal: total => `共 ${total} 条数据`,
          showQuickJumper: true,
          showSizeChanger: true,
          pageSizeOptions: ['5', '10', '15', '20'],
          onChange:(page,pageSize)=>this.getFansList(page,pageSize),//点击页码事件
        },
        followlist: [],
        fanList: [],
        isLoading: false,
        isFanLoading: false
      }
    },
    created() {
      //获取关注列表
      this.getFollowsList()
      //获取粉丝列表
      this.getFansList()
    },
    methods: {
      selectStyle(){

      },
      //获取关注列表
      getFollowsList(pageNum=1, pageSize=20){
        this.isLoading = true
        let params = {
          pageNum: pageNum,
          pageSize: pageSize,
          type: 0,
          userName: window.localStorage.getItem('userName'),
        }
        getFollowList(params)
          .then((res) => {
            if (res.status == 200){
              this.followlist = res.data.list
              this.pagination.total = res.data.total
              this.isLoading = false
            }
            else{
              this.$message.info(res.msg)
            }
          })
      },
      //获取粉丝列表
      getFansList(pageNum=1, pageSize=20){
        this.isFanLoading = true
        let params = {
          pageNum: pageNum,
          pageSize: pageSize,
          type: 1,
          userName: window.localStorage.getItem('userName'),
        }
        getFollowList(params)
          .then((res) => {
            if (res.status == 200){
              this.fanList = res.data.list
              this.fanPagination.total = res.data.total
              this.isFanLoading = false
            }
            else{
              this.$message.info(res.msg)
            }
          })
      },
      //取消关注粉丝
      cancelFollowed(item) {
        let params = {
          followUserName: window.localStorage.getItem('userName'),
          followedUserName: item.userName
        }
        cancelFollowResource(params)
          .then((res) => {
            if (res.status == 200){
              this.$message.success('取消关注成功！')
              this.getFollowsList()
              this.getFansList()
            }
            else{
              this.$message.info(res.msg)
            }
          })
      },
      //关注粉丝
      follow(item) {
        let params = {
          followUserName: window.localStorage.getItem('userName'),
          followedUserName: item.resourceUserName
        }
        followResource(params)
            .then((res) => {
              if (res.status == 200){
                this.$message.success('关注成功！')
                this.getFollowsList()
                this.getFansList()
              }
              else{
                this.$message.info(res.msg)
              }
            })
      },
    }
  }
</script>

<style scoped>
  .flex{
    display: flex;
    flex-direction: row;
  }
  .container{
    width: 1032px;
    height: 100%;
    background-color: #ffffff;
    border-radius: 4px;
    border: 1px solid #eeeeee;
    padding: 5px 13px;
  }
  .contain-header{
    justify-content: space-between;
    align-items: center;
  }
  .contain-title{
    font-family: MicrosoftYaHei;
    font-size: 18px;
    font-weight: bold;
    font-stretch: normal;
    letter-spacing: 1px;
    color: #1bcbb4;
    margin-top: 10px;
  }
  .tag-page{
    margin-top: 30px;
    text-align: left;
  }
  .avatar{
    border: 2px solid #1bcbb4;
    width: 80px;
    height: 80px;
  }
  .title{
    display: flex;
    flex-direction: row;
    align-content: center;
  }
  .header{
    display: flex;
    flex-direction: row;
    justify-content: space-between;
  }
  .course{
    font-family: MicrosoftYaHei;
    font-size: 12px;
    color: #999999;
    margin-left: 15px;
    margin-right: 15px;
  }
  .followed{
    margin-left: 98px;
    margin-top: -10px;
  }
  .image{
    width: 194px;
    height: 118px;
    cursor: pointer;
  }
  .more{
    display: inline-block;
    font-size: 13px;
    cursor: pointer;
  }
  .more-list{
    margin-top: -10px;
    margin-bottom: 5px;
    text-align: right;
  }
  .more:hover{
    color: #1bcbb4;
  }
  .column-space{
    margin-top: 30px;
  }
</style>